define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {
                    listData: [],
                    loading:0,
                    editData:{},
                    showNewAddress:0,
                    area:[],
                    type:1,
                    isWeiXin:0,
                    nodata:0

                };
            },
            el: '#pageAddress',
            created: function() {
                this.fetchData();
            },
             mounted: function() {
                    
                    if (this.mounted == 1) {
                        return;
                    }
                    var that = this;
                    app.cityPicker("#areaAddress",{
                            title: "选择地区",
                            onChange: function (picker, values, displayValues) {
                              console.log( values);
                                console.log( displayValues);
                                that.area = displayValues;
                                // that.cityName = displayValues[1];
                                // that.countyName = displayValues[2];
                            }
                    });
                    
                this.mounted = 1;

            },

            methods: {
                fetchData: function() {
                    var _this = this;
                    this.getList();
                    this.type = this.$route.params.type;
                    this.isWeiXin =  app.isWeiXin();
                    this.isWeiXin =  app.isWeiXin();
                },
                getList:function(){
                  var that = this;
                  if(that.loading==1){
                    return ;
                  }
                  that.loading =1;
                  app.get("mall/user_address/get", {
                  }, function (res) {
                      that.loading = 0;
                      if (res.code != 200) {
                        app.alert(res.msg);
                        return;
                      }
                      if(res.data.list.length===0){
                        that.nodata=1;
                      }else{
                        that.nodata=0;
                      }
                      that.listData=res.data.list;  
                      // console.log(that.listData);
                    });
                },


                // 微信中选择地址
                openAddress:function(){

                   if(!app.isWeiXin()){
                     return ;
                   }
                   var that = this;
                   app.wxJs(function(wx) {
                    
                      wx.openAddress({
                        success: function (res) {
                          console.log(res);
                          that.editData = res;
                          that.area =[res.provinceName,res.cityName,res.countryName];
                            // console.log(res);
                            // var userName = res.userName; // 收货人姓名
                            // var postalCode = res.postalCode; // 邮编
                            // var provinceName = res.provinceName; // 国标收货地址第一级地址（省）
                            // var cityName = res.cityName; // 国标收货地址第二级地址（市）
                            // var countryName = res.countryName; // 国标收货地址第三级地址（国家）
                            // var detailInfo = res.detailInfo; // 详细收货地址信息
                            // var nationalCode = res.nationalCode; // 收货地址国家码
                            // var telNumber = res.telNumber; // 收货人手机号码
                         },
                          fail:function(res){
                            app.alert(res.errMsg);
                          }
                        });
                   });
                },
                newAddress:function(index){
                  var that = this;
                  // console.log(index);
                  that.area =[];
                  if(typeof(index)!="undefined"){
                    this.editData = that.listData[index];
                  }else{
                    this.editData ={};
                  }
                  this.showNewAddress=1;
                },
                saveAddress:function(){
                   var data = $("#pageAddress").find("#formNewAddress").serializeArray();
                   if(this.area.length==3){
                      data.push({
                       name:'provinceName',
                       value:this.area[0],
                     });
                     data.push({
                       name:'cityName',
                       value:this.area[1],
                     });
                     data.push({
                       name:'countryName',
                       value:this.area[2],
                     });
                   }
                   var that = this;
                  if(that.loading==1){
                    return ;
                  }
                  that.loading =1;
                  app.post("mall/user_address/add", data, function (res) {
                    that.loading = 0;
                    if (res.code != 200) {
                      app.alert(res.msg);
                      return;
                    }
                     app.setPageData("address",{address:res.data});
                     if(that.type==1){
                       that.$router.go(-1);
                     }
                  });
                   
                  
                },
                selectAddress:function(index){
                  // console.log(index);
                   var data = this.listData[index];
                   app.setPageData("address",{address:data});
                   if(this.type==1){
                       this.$router.go(-1);
                   }
                },
                hideNewAddress:function(){
                   this.showNewAddress=0;
                },
                delAddress:function(){
                  var that = this;
                  that.loading =1;
                  app.post("mall/user_address/del", {
                    id: this.editData.id
                  }, function (res) {
                    that.loading = 0;
                    if (res.code != 200) {
                      app.alert(res.msg);
                      return;
                    }
                    that.getList();
                    that.hideNewAddress();
                  });
                }
 
            }

        }).$mount('#app');
    };
    module.exports = controller;
});
